package src.main.zhongjiezhe;
/**中介者模式*/
public class Test002 {
        public static void main(String[] args) {
            ConcreteMediator mediator=new ConcreteMediator();//定义中介者
            //将通信者与中介者关联起来
            ColleagueA colleagueA=new ColleagueA(mediator);
            ColleagueB colleagueB=new ColleagueB(mediator);
            ColleagueC colleagueC=new ColleagueC(mediator);
            //设置通信者信息
            colleagueA.setName("A国");
            colleagueB.setName("B国");
            colleagueC.setName("C国");
            String[] messA={"要求归还曾经抢夺的100斤土豆","要求归还曾经抢夺的20头牛"};
            colleagueA.giveMess(messA);
            System.out.println();
            String[] messB={"要求归还曾经抢夺的10只公鸡","要求归还曾经抢夺的15匹马"};
            colleagueB.giveMess(messB);
            System.out.println();
            String[] messC={"要求归还曾经抢夺的300斤小麦","要求归还曾经抢夺的50头驴"};
            colleagueC.giveMess(messC);
        }
}


/**中介类，设置通信者之间的通信规则*/
class ConcreteMediator {
    ColleagueA colleagueA;
    ColleagueB colleagueB;
    ColleagueC colleagueC;
    public void registerColleagueA(ColleagueA colleagueA) {
        this.colleagueA = colleagueA;
    }
    public void registerColleagueB(ColleagueB colleagueB) {
        this.colleagueB = colleagueB;
    }
    public void registerColleagueC(ColleagueC colleagueC) {
        this.colleagueC = colleagueC;
    }
    /**根据发送者，设置消息接收的规则，Colleague colleague 消息发送者，String[] mess 消息数组*/
    public void deliverMess(Colleague colleague,String[] mess){
        if(colleague==colleagueA){
            if(mess.length>=2){
                colleagueB.receiveMess(colleague.getName()+mess[0]);
                colleagueC.receiveMess(colleague.getName()+mess[1]);
            }
        }else if(colleague==colleagueB){
            if(mess.length>=2){
                colleagueA.receiveMess(colleague.getName()+mess[0]);
                colleagueC.receiveMess(colleague.getName()+mess[1]);
            }
        }else if(colleague==colleagueC){
            if(mess.length>=2){
                colleagueA.receiveMess(colleague.getName()+mess[0]);
                colleagueB.receiveMess(colleague.getName()+mess[1]);
            }
        }
    }
}

/**通信者的接口*/
interface Colleague {
    public void giveMess(String[] mess);//发送的消息
    public void receiveMess(String mess);//接收到的消息
    public void setName(String name);
    public String getName();
}
/**具体通信者*/
class ColleagueA implements Colleague{
    ConcreteMediator mediator;//中介者
    String name;
    public ColleagueA(ConcreteMediator mediator) {
        this.mediator = mediator;
        mediator.registerColleagueA(this);//将此对象存入中介
    }
    /**存入想要发送的消息String[] mess*/
    @Override
    public void giveMess(String[] mess) {
        mediator.deliverMess(this,mess);
    }
    /**收到的其他通信者发来的消息String mess*/
    @Override
    public void receiveMess(String mess) {
        System.out.println(name+"收到的信息:");
        System.out.println("\t"+mess);
    }

    @Override
    public void setName(String name) {
        this.name=name;
    }

    @Override
    public String getName() {
        return name;
    }
}

class ColleagueB implements Colleague{
    ConcreteMediator mediator;//中介者
    String name;

    public ColleagueB(ConcreteMediator mediator) {
        this.mediator = mediator;
        mediator.registerColleagueB(this);
    }

    @Override
    public void giveMess(String[] mess) {
        mediator.deliverMess(this,mess);
    }

    @Override
    public void receiveMess(String mess) {
        System.out.println(name+"收到的信息:");
        System.out.println("\t"+mess);
    }

    @Override
    public void setName(String name) {
        this.name=name;
    }

    @Override
    public String getName() {
        return name;
    }
}

class ColleagueC implements Colleague{
    ConcreteMediator mediator;//中介者
    String name;

    public ColleagueC(ConcreteMediator mediator) {
        this.mediator = mediator;
        mediator.registerColleagueC(this);
    }

    @Override
    public void giveMess(String[] mess) {
        mediator.deliverMess(this,mess);
    }

    @Override
    public void receiveMess(String mess) {
        System.out.println(name+"收到的信息:");
        System.out.println("\t"+mess);
    }

    @Override
    public void setName(String name) {
        this.name=name;
    }

    @Override
    public String getName() {
        return name;
    }
}
